﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
	<head>
		<title>Rollup Configuration</title>
        <style type="text/css">
        *
        {
            padding: 0px;
            margin: 0px;
            border-width: 0px;
        }
        input {
            height:25px;
            widows:300px;
        }
        select {
            height:25px;
            widows:400px;
        }
        body
        {
            background-color: #F6F8FA;
            font-size: 12px;
            font-family: Segoe UI;
            color: #3B3B3B;
            margin-left:5px;
            margin-top:5px;
        }
        table
        {
            background-color: White;
            font-size: 12px;
            font-family: Segoe UI;
            color: #3B3B3B;
            vertical-align: top;
            border-collapse: collapse;
            border-color: #A5ACB5;
            border-width: 1pt;
            border-style: solid;
            width:auto;
            height:auto;
        }
        td
        {
            background-color: White;
            font-size: 12px;
            font-family: Segoe UI;
            color: Black;
            vertical-align: top;
            border-color: #A5ACB5;
            border-width: 1pt;
            border-style: solid;
            padding: 5px;
        }
        th
        {
            background-color: #EFF2F6;
            font-size: 12px;
            font-family: Segoe UI;
            font-weight: 600;
            color: #3B3B3B;
            vertical-align: top;
            border-color: #A5ACB5;
            border-width: 1pt;
            border-style: solid;
            height: 20px;
            padding: 5px;
            text-align: left;
        }
        p
        {
            margin-bottom: 10px;
        }
        ol
        {
            margin-left: 35px;
        }
        li
        {
            padding: 0 0 10px 0px;
        }
        .headerCss
        {
            background-color: #EFF2F6; font-size: 12px; font-family: Segoe UI; font-weight:600; color: #3B3B3B; 
            vertical-align:top; border-color:#A5ACB5; border-width:1pt; border-style:solid; height:20px; padding:5px; text-align:left;
            width:30%;
        }
        .seperator
        {
            background-color: White;
            font-size: 12px;
            font-family: Segoe UI;
            color: Black;
            vertical-align: top;
            border-color: #A5ACB5;
            border-width: 0px;
            border-style: solid;
            padding: 5px;
            width:5%;
        }
        .error
        {
            color:Red;
            font-weight:bold;
        }
            .tableRows {
                width:300px;
            }
        </style>
        <script src="Generic_Rollup_SDK_Script.js" type="text/javascript"></script>
        <script src="jquery_1.6.3.js" type="text/javascript"></script>
        <script src="sdk.jquery.js" type="text/javascript"></script>
        <script type="text/javascript" src="../ClientGlobalContext.js.aspx"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                $('#parentEntityDDL').change(function () {
                    //alert($(this).val()); // or $(this).val()
                    //alert($(this).find("option:selected").attr('ReferencingAttribute'));

                    //parentEntityIdColumn
                    $("#parentEntityIdColumn").val($(this).val());
                    //lookupIdColumn
                    $("#lookupIdColumn").val($(this).find("option:selected").attr('ReferencingAttribute'));

                    //ReferencedEntity
                    var parentEntityName = $(this).find("option:selected").attr('ReferencedEntity');
                    //alert(parentEntityName);
                    popParentEntityColumnsDDL(parentEntityName);

                });
                $('#parentEntityColumnsDDL').change(function () {
                    //parentEntityAttributeName
                    $("#parentEntityAttributeName").val($(this).val());
                });
            });

            function popChildEntityLogicalName() {
                if (this.parent.Xrm.Page.getAttribute("mkisv_name").getValue() != null) {
                    $("#childEntityLogicalName").val(this.parent.Xrm.Page.getAttribute("mkisv_name").getValue())

                    $("#rollupConfigId").val(this.parent.Xrm.Page.data.entity.getId());
                    popParentEntityDDL(this.parent.Xrm.Page.getAttribute("mkisv_name").getValue());
                }
                else {
                    //alert("NULL childEntityLogicalName");
                }
            }

            function popParentEntityDDL(entityLogicalName) {
                successCallback = function (responseText) {
                    //alert(responseText);
                    var xmlDoc = $.parseXML(responseText);
                    $xml = $(xmlDoc);

                    $("#childEntityIdColumn").val($xml.find("c\\:PrimaryIdAttribute, PrimaryIdAttribute").text());

                    $xml.find("c\\:ManyToOneRelationships, ManyToOneRelationships").find("c\\:OneToManyRelationshipMetadata, OneToManyRelationshipMetadata").each(function () {
                        //c:SecurityTypes
                        if ($(this).find("c\\:SecurityTypes, SecurityTypes").text() != "None" && $(this).find("c\\:SecurityTypes, SecurityTypes").text() != "Pointer") {
                            //c:ReferencedEntity
                            //alert($(this).find("c\\:SecurityTypes, SecurityTypes").text() + " -- " + $(this).find("c\\:ReferencedEntity, ReferencedEntity").text())

                            //c:ReferencedEntity
                            var ReferencedEntity = $(this).find("c\\:ReferencedEntity, ReferencedEntity").text();
                            //c:ReferencedAttribute
                            var ReferencedAttribute = $(this).find("c\\:ReferencedAttribute, ReferencedAttribute").text();

                            c: ReferencingAttribute
                            var ReferencingAttribute = $(this).find("c\\:ReferencingAttribute, ReferencingAttribute").text();


                            $("#parentEntityDDL").append("<option value='" + ReferencedAttribute + "' ReferencingAttribute='" + ReferencingAttribute + "' ReferencedEntity='" + ReferencedEntity + "'>" + ReferencedEntity + " (" + ReferencingAttribute + ")" + "</option>")
                        }
                    });

                    $('#parentEntityDDL').trigger('change');

                    $xml.find("c\\:Attributes, Attributes").find("c\\:AttributeMetadata, AttributeMetadata").each(function () {
                        //c:AttributeType : Money | Double | Integer | Decimal 

                        //alert($(this).find("c\\:AttributeType, AttributeType").text());

                        var AttributeType = $(this).find("c\\:AttributeType, AttributeType").text();

                        if (AttributeType == "Money" ||AttributeType == "Double" ||AttributeType == "Integer" ||AttributeType == "Decimal") {
                            //c:LogicalName
                            var LogicalName = $(this).find("c\\:LogicalName, LogicalName").text();
                            var DisplayName = $(this).find("c\\:DisplayName, DisplayName").find("a\\:LocalizedLabels , LocalizedLabels").find("a\\:Label, Label").text()

                            $("#childEntityAttributeName").append("<option value='" + LogicalName + "' AttributeType='" + AttributeType + "'>" + DisplayName+ " [" + LogicalName  + "]</option>")
                        }
                    });

                    $("#childEntityAttributeName").append("<option value='" + $xml.find("c\\:PrimaryIdAttribute, PrimaryIdAttribute").text() + "' AttributeType='Integer'>[" + $xml.find("c\\:PrimaryIdAttribute, PrimaryIdAttribute").text() + "]</option>")

                },
                errorCallback = function (msg) {
                    alert(msg);
                }
                SDK.GenericRollup.RetrieveEntityRequestRequest(entityLogicalName, successCallback, errorCallback);
            }

            function popParentEntityColumnsDDL(entityLogicalName) {
                $('#parentEntityColumnsDDL').find('option').remove();

                successCallback = function (responseText) {
                    //alert(responseText);
                    var xmlDoc = $.parseXML(responseText);
                    $xml = $(xmlDoc);

                    $xml.find("c\\:Attributes, Attributes").find("c\\:AttributeMetadata, AttributeMetadata").each(function () {
                        //c:AttributeType : Money | Double | Integer | Decimal 

                        //alert($(this).find("c\\:AttributeType, AttributeType").text());

                        if ($(this).find("c\\:AttributeType, AttributeType").text() == $("#childEntityAttributeName").find("option:selected").attr('AttributeType')) {

                            //c:LogicalName
                            var LogicalName = $(this).find("c\\:LogicalName, LogicalName").text();

                           // alert(LogicalName);

                            $("#parentEntityColumnsDDL").append("<option value='" + LogicalName + "'>" + LogicalName + "</option>")
                        }
                    });

                },
                errorCallback = function (msg) {
                    alert("Error in popParentEntityColumnsDDL " + msg);
                }
                SDK.GenericRollup.RetrieveParentEntityRequestRequest(entityLogicalName, successCallback, errorCallback);
            }

            function updateRollupConfig() {
                //alert("updateRollupConfig");
                var mkisv_rollupconfiguration = {};
                mkisv_rollupconfiguration.mkisv_name = $("#childEntityLogicalName").val();
                //alert(mkisv_rollupconfiguration.mkisv_name);
                mkisv_rollupconfiguration.mkisv_ChildEntityAttributeName = $("#childEntityAttributeName").val();
                //alert(mkisv_rollupconfiguration.mkisv_ChildEntityAttributeName);
                mkisv_rollupconfiguration.mkisv_ChildEntityIdAttribute = $("#childEntityIdColumn").val();
                //alert(mkisv_rollupconfiguration.mkisv_ChildEntityIdAttribute);
                mkisv_rollupconfiguration.mkisv_LookupAttributeName = $("#lookupIdColumn").val();
                //alert(mkisv_rollupconfiguration.mkisv_LookupAttributeName);
                mkisv_rollupconfiguration.mkisv_ParentEntityAttributeName = $("#parentEntityAttributeName").val();
                //alert(mkisv_rollupconfiguration.mkisv_ParentEntityAttributeName);
                mkisv_rollupconfiguration.mkisv_ParentEntityIdAttribute = $("#parentEntityIdColumn").val();
                //alert(mkisv_rollupconfiguration.mkisv_ParentEntityIdAttribute);
                mkisv_rollupconfiguration.mkisv_ParentEntityName = $("#parentEntityDDL").find("option:selected").attr('ReferencedEntity');
                //alert(mkisv_rollupconfiguration.mkisv_ParentEntityName);

                SDK.JQuery.updateRecord(
                    $("#rollupConfigId").val(),
                    mkisv_rollupconfiguration,
                    "mkisv_rollupconfiguration",
                    function () {
                        alert("The RollupConfig record changes were saved");
                    },
                    function (error) {
                        alert(error.message);
                    }
                  );
            }

        </script>
	</head>
	<body onload="popChildEntityLogicalName();">
	<div>
        <table>
            <tr>
                <td class="tableRows">Child Entity Logical Name : </td>
                <td><input type="text" name="childEntityLogicalName"  id="childEntityLogicalName"/></td>
            </tr>
            <tr>
                <td class="tableRows">Child Entity Aggregate Attribute Name :</td>
                <td><select name="childEntityAttributeName" id="childEntityAttributeName"></select></td>
            </tr>
            <tr>
                <td class="tableRows">Parent Entity Names (Lookup Attribute) :</td>
                <td><select name="parentEntityDDL" id="parentEntityDDL"></select></td>
            </tr>
            <tr>
                <td class="tableRows">Parent Entity Aggregate Attribute Name :</td>
                <td><select name="parentEntityColumnsDDL" id="parentEntityColumnsDDL"></select></td>
            </tr>
            <tr>
                <td class="tableRows"><input type="button" value="Save" id="submitChanges" name="submitChanges" onclick="updateRollupConfig();" /></td>
                <td></td>
            </tr>
        </table>
        <input type="hidden" name="rollupConfigId" id="rollupConfigId" /><input type="hidden" name="childEntityIdColumn" id="childEntityIdColumn" /><br />
        <input type="hidden" name="lookupIdColumn" id="lookupIdColumn" /><br />
        <input type="hidden" name="parentEntityIdColumn" id="parentEntityIdColumn" /><input type="hidden" name="parentEntityAttributeName" id="parentEntityAttributeName" />
	</div>
	</body>
</html>